package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.Shop;
import com.guoyansoft.model.pojo.ShopType;
import com.guoyansoft.model.service.ShopService;
import com.guoyansoft.model.service.ShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
private ShopService shopService;
    @Autowired
private ShopTypeService shopTypeService;

    @RequestMapping("/queryByPage")
    public R queryByPage(Integer curPage, Integer pageSize,
                         String shopTypeName) {
        Map<String,Object> params=new HashMap<>();
        params.put("shopTypeNameKey","%"+shopTypeName+"%");
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        R r = shopService.selectByPageAndTotalsize(params);
        return r;
    }
    @RequestMapping("/save")
    public R save(Shop shop, Integer shopTypeId) {
        ShopType shopType=new ShopType();
        shopType.setId(shopTypeId);
        shop.setShopType(shopType);
        boolean flag =  shopService.insert(shop);
        return flag?R.suc():R.fail();
    }
    @RequestMapping("/update")
    public R update(Shop shop,Integer shopTypeId){
        ShopType shopType=new ShopType();
        shopType.setId(shopTypeId);
        shop.setShopType(shopType);
        boolean flag = shopService.update(shop);
        return flag?R.suc():R.fail();
    }

    @RequestMapping("/queryById")
    public R queryById(Integer id){
        R shop = shopService.selectById(id);
        return shop;
    }
    @RequestMapping("/queryShopType")
    public R queryShopType(){
        List<ShopType> shopTypes = shopTypeService.queryAll();
        return shopTypes==null?R.fail():R.suc(shopTypes);
    }

    @RequestMapping("/queryAll")
    public R queryAll( Integer curPage, Integer pageSize,String shopName){
        Map<String,Object> params=new HashMap<>();
        params.put("shopNameKey","%"+(shopName==null?"":shopName)+"%");
        if(curPage!=null && pageSize!=null){
            params.put("startIndexKey",(curPage-1)*pageSize);
            params.put("pageSizeKey",pageSize);
        }

        R r=shopService.selectAll(params);
        return r;
    }

//    @RequestMapping("/dropBatch")
//    public R dropBatch(Integer id) {
//        boolean flag = shopService.delete(id);
//        return flag?R.suc():R.fail();
//    }
//    @RequestMapping("/update")
//    public R update(Shop shop){
//        boolean flag = shopService.update(shop);
//        return flag?R.suc():R.fail();
//    }


}
